package cn.starrysky108.simpleerp.user.controller;


import cn.starrysky108.simpleerp.core.base.BaseResponseEntity;
import cn.starrysky108.simpleerp.core.security.UserDetailsEntity;
import cn.starrysky108.simpleerp.core.util.JwtUtil;
import cn.starrysky108.simpleerp.pub.request.PageParam;
import cn.starrysky108.simpleerp.user.entity.OnlineDeviceInfo;
import cn.starrysky108.simpleerp.user.entity.User;
import cn.starrysky108.simpleerp.user.service.UserService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author Carl Lee
 * @since 2021-08-14
 */
@RestController
@Api(tags = "用户信息管理")
@RequestMapping("/users")
public class UserController {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/{id}")
    @ApiOperation("获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", dataTypeClass = Long.class)
    public User getUserInfo(@PathVariable Long id) {
        assert id.toString().length() == 18;
        User user = userService.getById(id);
        user.setPassword("****");
        return user;
    }

    @GetMapping("/page")
    @ApiOperation("获取所有用户信息，分页")
    public IPage<User> listByPage(PageParam param) {
        Page<User> infos = new Page<>(param.getCurrent(), param.getSize());
        return userService.page(infos, null);
    }

    @GetMapping(value = "/me")
    @ApiOperation("获取当前登录账户的信息")
    public BaseResponseEntity<User> getActiveUser(@ApiIgnore Authentication authentication) {
        UserDetailsEntity userDetails = (UserDetailsEntity) authentication.getPrincipal();
        User user = userDetails.getUser();
        //隐藏密码
        user.setPassword("****");
        return BaseResponseEntity.success(user);
    }

    @PostMapping("/ids")
    @ApiOperation("列出给定id对应的用户,接收json数据")
    public List<User> getUsers(@RequestBody Long[] ids) {
        return Arrays.stream(ids)
                //过滤掉不合法的id
                .filter(id -> id.toString().length() == 18)
                .map(id -> {
                    //查询用户信息后隐藏
                    User user = userService.getById(id);
                    user.setPassword("***");
                    return user;
                }).collect(Collectors.toList());

    }

    @GetMapping("/device/list")
    @ApiOperation(value = "获取在线设备信息")
    public BaseResponseEntity<Map<String, OnlineDeviceInfo>> listOnlineDevice(@ApiIgnore HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request);
        return BaseResponseEntity.success(userService.listOnlineDevice(userId));

    }
}
